package com.gec.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.gec.entity.User;
import com.gec.service.UserService;
public class LoginServlet extends HttpServlet {
    
    UserService userService = new UserService();
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受用户的请求 判断账号密码是否正确
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        User user;	
        try {
            user = userService.login(username, password);
            if(user!=null) {
                HttpSession session = request.getSession();
                session.setAttribute("user",user);
                response.sendRedirect("IndexServlet");
            }else {
                request.setAttribute("error", "账号或密码输入错误，请重新输入！");
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", "登录异常，请稍后再试！");
            request.getRequestDispatcher("login.jsp").forward(request, response);
        }
    }
}
